/******************************************************************************************************************
* File:SinkFilter.java
* Course: 17655
* Project: Assignment 1
* Copyright: Copyright (c) 2003 Carnegie Mellon University
* Versions:
*	1.0 November 2008 - Sample Pipe and Filter code (ajl).
*
* Description:
*
* This class serves sink filter. This particular
* filter reads some input from the filter's input port and does the following:
* 
* - Writes a header to the file
* - Writes the input stream to the file (according to specified format) 
******************************************************************************************************************/
import java.io.FileWriter;
import java.io.IOException;

public class SinkFilter extends FilterFramework2
{
	// Internal representation for input parameters
	private int[] typesToFilter;
	private String filename;
	

	// Constructor
	// Take types for filtering specificantion and a name of output file.
	public SinkFilter(int[] typesToFilter_, String filename_)
	{
		super(1,0); // one input port, zero outputs
		typesToFilter = typesToFilter_;
		filename=filename_;
	}
	
	public void run()
    {
		// Initialize
		System.out.print( "\n" + this.getName() + "::Sink Reading ");

		FileWriter out = null;
		Measurement mes = new Measurement();
		
		String cleanFileHeader = null;
		cleanFileHeader = dataHeader(typesToFilter);
		
		// Write header to the file
		try {
			out = new FileWriter(filename);
			out.write(cleanFileHeader);
		} catch(IOException e)
		{
			System.out.print( "\n" + this.getName() + "File ::Sink Exiting;");
		}
		
		while (true)
		{
			try
			{
				// Read from stream and send to file
				mes.readByteStream(this, 0);
			    out.write(mes.toString(typesToFilter));
			    out.flush();
			}
			catch (EndOfStreamException e)
			{
				System.out.print( "\n" + this.getName() + "::Sink 1 Exiting;");
				break;

			}
			catch (IOException e)
			{
				System.out.print( "\n" + this.getName() + "::Sink 2 Exiting;");
				break;

			} // catch
			catch (ClassNotFoundException e)
			{
				System.out.print( "\n" + this.getName() + "::Sink 3 Exiting;");
				break;

			}
		}
	}
	
	// dataHeader Method 
	// Creates a pretty-printed header for an output file
	public String dataHeader(int[] typestofilter){
		
		String header = "";
		
		for(int i=0; i < typestofilter.length ; i++)
		{
			// Cycle through every ID
			switch(typestofilter[i]) 
			{
			case 0:
				header = header.concat("      TIME      ");
				break;
			case 1:
				header = header.concat("         VELOCITY   ");
				break;
			case 2:
				header = header.concat("       ALTITUDE   ");
				break;
			case 3:
				header = header.concat("       PRESSURE   ");
				break;
			case 4:
				header = header.concat("       TEMPERATURE  ");
				break;
			case 5:
				header = header.concat("      ATTITUDE   ");
				break;
			}
		}
		
		return header.concat("\r\n");
	}	
}	// SinkFilter
			